<?php
require_once ("../utils/ConnectionFactory.php");
require_once ("../persistance/data/ArticleDO.php");

/**
 * CRUD pour les articles
 */
class ArticleDAO {
	
	/**
	 * donne la liste des articles
	 */
	public function getListArticleDO() {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT * FROM article" );
		
		if ($reponse == null) {
			return null;
		}
		
		$listArticleDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$articleDO = $this->setArticleDO ( $data );
			$listArticleDO->append ( $articleDO );
		}
		return $listArticleDO;
	}
	
	/**
	 * donne la liste des types articles
	 */
	public function getListTypeArticleDO() {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT distinct libelle_type_art FROM article" );
		
		if ($reponse == null) {
			return null;
		}
		
		$listTypeArticleDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$typeArticleDO = $this->setTypeArticleDO ( $data );
			$listTypeArticleDO->append ( $typeArticleDO );
		}
		return $listTypeArticleDO;
	}
	
	/**
	 * retourne un articleDO en fonction de son id
	 */
	public function getArticleDO($numeroArticle) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT * FROM article WHERE num_article =" . $numeroArticle );
		if ($reponse == null) {
			return null;
		}
		
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$articleDO = $this->setArticleDO ( $data );
		}
		return $articleDO;
	}
	
	/**
	 * retourne un articleDO en fonction de son nom
	 */
	public function getArticleByNameDO($nomArticle) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT * FROM article WHERE libelle = '$nomArticle'" );
		if ($reponse == null) {
			return null;
		}
		
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$articleDO = $this->setArticleDO ( $data );
		}
		return $articleDO;
	}
	
	/**
	 * retourne un articleDO en fonction de son id promo
	 */
	public function getArticlePromoDO($numeroPromo) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT article.num_article, article.`libelle`,`pu`,`libelle_type_art`,`quantite` FROM `article` join promotion on article.num_article = promotion.num_article where promotion.num_promo = $numeroPromo" );
		if ($reponse == null) {
			return null;
		}
		
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$articleDO = $this->setArticleDO ( $data );
		}
		return $articleDO;
	}
	private function setArticleDO($donnee) {
		$articleDO = new ArticleDO ();
		$articleDO->setNumArticle ( $donnee ['num_article'] );
		$articleDO->setLibelle ( $donnee ['libelle'] );
		$articleDO->setPrixUnitaire ( $donnee ['pu'] );
		$articleDO->setLibelleTypeArticle ( $donnee ['libelle_type_art'] );
		$articleDO->setQuantite ( $donnee ['quantite'] );
		
		return $articleDO;
	}
	private function setTypeArticleDO($donnee) {
		$typeArticleDO = new TypeArticleDO ();
		$typeArticleDO->setLibelleTypeArticle ( $donnee ['libelle_type_art'] );
		return $typeArticleDO;
	}
}	




